// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package edit
{
    import flash.desktop.ClipboardFormats;
    import flash.desktop.NativeDragManager;
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.NativeDragEvent;
    import flash.filesystem.File;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;

    public class EditScaleImgSourceComponent
    {
        private var mLayerMc:MovieClip;

        private var mImgLoader:Loader;
        private var mTargetImg:Bitmap;

        private var onSourceDropInCallback:Function;

        public function EditScaleImgSourceComponent()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_layerMc:MovieClip, _onSourceDropInCallback:Function):void
        {
            mLayerMc = _layerMc;
            onSourceDropInCallback = _onSourceDropInCallback;
            addListener();
        }

        public function getImgBounds():Rectangle
        {
            if (mTargetImg != null)
            {
                return mTargetImg.getBounds(mTargetImg.parent);
            } else
            {
                return null;
            }
        }

        public function isHaveImg():Boolean {return mTargetImg != null}

        public function getTargetImg():Bitmap {return mTargetImg;}

        //==================================
        //== Private Function
        //==================================

        private function addListener():void
        {
            mLayerMc.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onFileDragIn);
            mLayerMc.stage.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onFileDrop);
        }

        private function onFileDragIn(e:NativeDragEvent):void
        {
            NativeDragManager.acceptDragDrop(mLayerMc.stage);
        }

        private function onFileDrop(e:NativeDragEvent):void
        {
            mTargetImg && mTargetImg.parent.removeChild(mTargetImg);
            var dropFile:File = (e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array)[0];

            mImgLoader = new Loader();
            mImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoadComplete);
            mImgLoader.load(new URLRequest(dropFile.url));
        }

        private function onImgLoadComplete(event:Event):void
        {
            mTargetImg = Bitmap(mImgLoader.content);
            mTargetImg.x = (mLayerMc.width - mTargetImg.width) / 2;
            mTargetImg.y = (mLayerMc.height - mTargetImg.height) / 2;

            mLayerMc.addChild(mTargetImg);
            onSourceDropInCallback();
        }


    }
}
